package com.ou.csbs.capstone.tools;

import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.render.BasicShapeAttributes;
import gov.nasa.worldwind.render.Material;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import com.ou.csbs.capstone.models.AttributeModel;
import com.ou.csbs.capstone.models.RenderableModel;
import com.ou.csbs.capstone.models.shapes.Line;
import com.ou.csbs.capstone.models.shapes.Text;
import com.ou.csbs.capstone.views.worldwind.WorldWindCanvas;
/**
 * This is the TextDrawingTool. During use, it will display a temporary black Line object that the text will be drawn across (operation is done in the same way as the LineDrawingTool). Once this Line is completed,
 * Text (that is specified with a JOptionPane prompt) will be drawn at the center point of this line with the current specified Color and Font size in the Attribute model. The text will be rotated to align with 
 * the direction the temporary Line object was drawn.  
 * 
 * @author Senior Capstone
 */
public class TextDrawingTool extends MouseAdapter {
	private Line tempLine;
	private Position begin, end;
	private RenderableModel model;
	private BasicShapeAttributes creationAttributes;

	public TextDrawingTool(RenderableModel model){
		this.model = model;
		this.creationAttributes = new BasicShapeAttributes();
		this.creationAttributes.setOutlineWidth(3);
		this.creationAttributes.setDrawInterior(false);
		this.creationAttributes.setOutlineMaterial(Material.BLACK);
	}

	@Override
	public void mousePressed(MouseEvent mouseEvent) {
		if(mouseEvent.getButton() == MouseEvent.BUTTON1){
			begin = WorldWindCanvas.getWorldWindow().getCurrentPosition();
			if(begin != null){
				tempLine = new Line(this.creationAttributes, this.begin, this.begin);
				tempLine.drawShape(this.model);
			}
		}
		mouseEvent.consume();
	}
	@Override
	public void mouseDragged(MouseEvent mouseEvent){
		if(SwingUtilities.isLeftMouseButton(mouseEvent)){
			Position position = WorldWindCanvas.getWorldWindow().getCurrentPosition();
			if(position != null){
				if(!tempLine.isDrawn(this.model)){
					tempLine.drawShape(this.model);
				}
				tempLine.updatePoint(1, position);
			}else{
				tempLine.removeShape(this.model);
			}
		}
		mouseEvent.consume();
	}
	@Override
	public void mouseReleased(MouseEvent mouseEvent) {
		if(mouseEvent.getButton() == MouseEvent.BUTTON1){
			end = WorldWindCanvas.getWorldWindow().getCurrentPosition();
			if(end != null){
				tempLine.removeShape(this.model);
				String input = JOptionPane.showInputDialog(null, "Text to draw:");

				//re-adjusting the calculated heading to be a rotational angle
				double angle = LatLon.linearAzimuth(begin, end).getDegrees();
				//adjust angle
				angle -= 90;

				double pixelSize = WorldWindCanvas.getWorldWindow().getView().computePixelSizeAtDistance(WorldWindCanvas.getWorldWindow().getView().getEyePosition().getAltitude());
				double fontSize = AttributeModel.getFontSize();
				
				Position midpoint = Position.interpolate(.5, begin, end);
				if(input != null){
					Text drawnText = new Text(input, midpoint, angle, pixelSize*fontSize, fontSize, AttributeModel.getAttributes().getOutlineMaterial().getDiffuse());
					drawnText.drawShape(this.model);				
				}
			}else{
				tempLine.removeShape(this.model);
			}
		}
		mouseEvent.consume();
	}
}
